<?php

namespace app\admin\controller;

use app\common\controller\AdminBase;

class Projectrecommend extends AdminBase
{
    protected function _initialize()
    {
        parent::_initialize();
        if ($this->request->isGet()) {
            $this->assign('institute', model('user')->field('institute')->group('institute')->select());
            $this->assign('jobTitle', model('user')->field('job_title')->group('job_title')->select());
        }
    }

    public function index()
    {
        $param = $this->request->param();
        $where = [];
        if (isset($param['name'])) {
            $where['name'] = ['like', "%" . $param['name'] . "%"];
        }
        if (isset($param['createFrom']) && isset($param['createTo'])) {
            $param['createFrom'] .= " 00:00:00";
            $param['createTo'] .= " 23:59:59";
            $where['create_time'] = array('between', array($param['createFrom'], $param['createTo']));
        } else {
            if (isset($param['createFrom'])) {
                $param['createFrom'] .= " 00:00:00";
                $where['create_time'] = array('egt', $param['createFrom']);
            }
            if (isset($param['createTo'])) {
                $param['createTo'] .= " 23:59:59";
                $where['create_time'] = array('elt', $param['createTo']);
            }
        }
        $list = model('projectRecommend')
            ->where($where)
            ->paginate(config('page_number'), false, ['query' => $param]);
        return $this->fetch('index', ['list' => $list]);
    }

    public function add()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $param['create_time'] = date('Y-m-d H:i:s');
            if ($this->insert('projectRecommend', $param, false) === true) {
                insert_admin_log('添加了项目推荐');
                $this->success('添加成功', url('admin/projectrecommend/index'));
            } else {
                $this->error($this->errorMsg);
            }
        }
        return $this->fetch('save', ['projectRecommend' => model('projectRecommend')->order('id asc')->select()]);
    }

    public function edit()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $param['update_time'] = date('Y-m-d H:i:s');
            if ($this->update('projectRecommend', $param, false, input('_verify', true)) === true) {
                insert_admin_log('修改了项目推荐');
                $this->success('修改成功', url('admin/projectrecommend/index'));
            } else {
                $this->error($this->errorMsg);
            }
        }
        return $this->fetch('save', [
            'data'     => model('projectRecommend')->where('id', input('id'))->find(),
            'projectRecommend' => model('projectRecommend')->order('id asc')->select(),
        ]);
    }

    public function del()
    {
        if ($this->request->isPost()) {
            if ($this->delete('projectRecommend', $this->request->param()) === true) {
                insert_admin_log('删除了项目推荐');
                $this->success('删除成功');
            } else {
                $this->error($this->errorMsg);
            }
        }
    }

    public function userIndex()
    {
        $param = $this->request->param();
        $where = [];
        $whereOr = [];
        if (isset($param['name'])) {
            $whereOr['user_name'] = ['like', "%" . $param['name'] . "%"];
            $whereOr['email'] = ['like', "%" . $param['name'] . "%"];
            $whereOr['phone'] = ['like', "%" . $param['name'] . "%"];
        }
        if (isset($param['institute'])) {
            $where['institute'] = $param['institute'];
        }
        if (isset($param['job_title'])) {
            $where['job_title'] = $param['job_title'];
        }
        if (isset($param['createFrom']) && isset($param['createTo'])) {
            $param['createFrom'] .= " 00:00:00";
            $param['createTo'] .= " 23:59:59";
            $where['create_time'] = array('between', array($param['createFrom'], $param['createTo']));
        } else {
            if (isset($param['createFrom'])) {
                $param['createFrom'] .= " 00:00:00";
                $where['create_time'] = array('egt', $param['createFrom']);
            }
            if (isset($param['createTo'])) {
                $param['createTo'] .= " 23:59:59";
                $where['create_time'] = array('elt', $param['createTo']);
            }
        }
        if (isset($param['useFrom']) && isset($param['useTo'])) {
            $param['useFrom'] .= " 00:00:00";
            $param['useTo'] .= " 23:59:59";
            $where['create_time'] = array('between', array($param['useFrom'], $param['useTo']));
        } else {
            if (isset($param['useFrom'])) {
                $param['useFrom'] .= " 00:00:00";
                $where['create_time'] = array('egt', $param['useFrom']);
            }
            if (isset($param['useTo'])) {
                $param['useTo'] .= " 23:59:59";
                $where['create_time'] = array('elt', $param['useTo']);
            }
        }
        $list = model('user')
            ->whereOr(function ($query) use ($whereOr) {
                $query->whereOr($whereOr);
            })
            ->where(function ($query) use ($where) {
                $query->where($where);
            })
            ->paginate(config('page_number'), false, ['query' => $param]);
        return $this->fetch('pushIndex', ['list' => $list, 'projectRecommendId' => $param['projectRecommendId']]);
    }

    public function choosePeople()
    {
        $param = $this->request->param();
        $whereIn = [];
        $projectRecommend = model('projectRecommend')->where('id', '=', $param['projectRecommendId'])->find();
        $list = [];
        if (!empty($projectRecommend)) {
            $whereIn['user_id'] = $projectRecommend['notice_people'];
            $list = model('user')
                ->whereIn('id', $projectRecommend['notice_people'])
                ->paginate(config('page_number'), false, ['query' => $param]);
        }
        return $this->fetch('choosePeople', ['list' => $list, 'projectRecommendId' => $param['projectRecommendId']]);
    }

}
